package com.qianbao.qianbaobusiness.ui.cashier.databill;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.DayBillDetailedBean;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 15:22 2017/4/25 04
 */

public class DataBillDetailedAdapter extends BaseRecyclerViewAdapter<DayBillDetailedBean, BaseViewHolder> {

    boolean type = true;

    public DataBillDetailedAdapter(Context context, List<DayBillDetailedBean> dayBillDetailedBeen) {
        super(context, dayBillDetailedBeen);
    }

    public void setRefreshAdapter(List<DayBillDetailedBean> dayBillDetailedBeen) {
        this.mDatas = dayBillDetailedBeen;
        notifyDataSetChanged();
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, DayBillDetailedBean model) {
        if (getItemViewType(position) == 0) {

            NoMoreholder noMoreholder = (NoMoreholder) holder;

            if(mDatas.size()==0){
                noMoreholder.noMore.setVisibility(View.GONE);
            }else{
                noMoreholder.noMore.setVisibility(View.VISIBLE);
                noMoreholder.noMore.setText("没有更多数据了");
            }

        } else {
            DataBillDetailedHolder dataBillDetailedHolder = (DataBillDetailedHolder) holder;
            setTextLeftImg(dataBillDetailedHolder.detailedTypeIv, model.getChkAccountType());
            if (!TextUtils.isEmpty(model.getOrderNo())) {
                dataBillDetailedHolder.orderTimeTv.setText(model.getOrderNo() + "\n" + model.getTradeDate());
            } else {
                dataBillDetailedHolder.orderTimeTv.setText(model.getReferNo() + "\n" + model.getTradeDate());
            }
            String settlementAmt = Double.parseDouble(model.getSettlementAmt() + "") / 100 + "";
            dataBillDetailedHolder.detailedAmountTv.setText("¥" + FormatUtil.getMoneyFormat(settlementAmt));
            String orderAmt = Double.parseDouble(model.getOrderAmt() + "") / 100 + "";
            String tradeAmt = Double.parseDouble(model.getTradeAmt() + "") / 100 + "";


            if(model.getChkAccountType() == 4){
                dataBillDetailedHolder.tv_old_balance.setText("原订单¥" + FormatUtil.getMoneyFormat(orderAmt));
            }else{
                dataBillDetailedHolder.tv_old_balance.setText("原订单¥"+ FormatUtil.getMoneyFormat(tradeAmt));
            }
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                View view0 = LayoutInflater.from(mContext).inflate(R.layout.item_no_more, parent, false);
                return new NoMoreholder(mContext, view0, this);
            case 1:
                View view = LayoutInflater.from(mContext).inflate(R.layout.activity_data_bill_detaile_item, parent, false);
                return new DataBillDetailedHolder(mContext, view, this, getClickListener(), getLongClickListener());
            default:
                return null;
        }

    }

    class DataBillDetailedHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener clickListener;
        public OnRecyclerViewItemLongClickListener longClickListener;
        public ImageView detailedTypeIv;
        public TextView orderTimeTv;
        public TextView detailedAmountTv;
        public TextView tv_old_balance;

        public DataBillDetailedHolder(Context context, View rootView, RecyclerView.Adapter adapter, OnRecyclerViewItemClickListener clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
            super(context, rootView, adapter, clickListener, longClickListener);
            detailedTypeIv = (ImageView) rootView.findViewById(R.id.bill_detailed_type_iv);
            orderTimeTv = (TextView) rootView.findViewById(R.id.bill_detailed_order_time_tv);
            detailedAmountTv = (TextView) rootView.findViewById(R.id.bill_detailed_amount_tv);
            tv_old_balance = (TextView) rootView.findViewById(R.id.tv_old_balance);
            this.clickListener = clickListener;
            this.longClickListener = longClickListener;
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (clickListener != null) {
                clickListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (longClickListener != null) {
                longClickListener.onItemLongClick(parent, view, position);
            }
        }
    }

    private void setTextLeftImg(ImageView imageView, int iconId) {
        int imgId = R.mipmap.weixin_icon;
        switch (iconId) {
            case 1:
                imgId = R.mipmap.yinhangka_icon;
                break;
            case 2:
                imgId = R.mipmap.zhifubao_icon;
                break;
            case 3:
                imgId = R.mipmap.weixin_icon;
                break;
            case 4:
                imgId = R.mipmap.qianbaoshenghuo_icon;
                break;
            default:
                break;
        }
        Drawable drawable = mContext.getResources().getDrawable(imgId);
        imageView.setImageDrawable(drawable);
    }

    @Override
    public int getItemViewType(int position) {
        if (type && position == getItemCount() - 1) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        if(type){
            return mDatas.size() +1;
        }else {
            return mDatas.size();
        }


    }

    public void setType(boolean type) {
        this.type = type;
    }

    class NoMoreholder extends BaseViewHolder {
        private TextView noMore;

        public NoMoreholder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            noMore = (TextView) rootView.findViewById(R.id.no_more_name);
        }
    }
}
